/*
 * @Author: 彭林
 * @Date: 2021-05-14 17:49:14
 * @LastEditTime: 2021-07-01 15:43:42
 * @LastEditors: 彭林
 * @Description:
 * @FilePath: /src/saas-app/app/api/v1/subject.go
 */
package v1

import (
	"saas-app/app/define"
	"saas-app/app/model"
	"saas-app/app/service"
	"saas-app/library/response"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

func GetSubjectList(r *ghttp.Request) {

	page := r.GetQueryInt("page")
	limit := r.GetQueryInt("limit")
	school_id := r.GetQueryInt("school_id")
	campus_id := r.GetQueryInt("campus_id")

	result, total := service.SubjectService().GetSubjectList(school_id, campus_id, page, limit)

	response.JsonExit(r, 0, "ok", response.Paging(result, total, page, limit))
}

func UpdateSubject(r *ghttp.Request) {

	subject_id := r.GetQueryUint("subject_id")

	var data = make(map[string]interface{})

	if r.GetForm("subject_name") != nil {
		data["subject_name"] = r.GetFormString("subject_name")
	}

	if r.GetForm("remark") != nil {
		data["remark"] = r.GetFormString("remark")
	}

	if r.GetForm("status") != nil {
		data["status"] = r.GetFormUint("status")
	}

	if r.GetForm("ks") != nil {
		data["ks"] = r.GetFormUint("ks")
	}

	if r.GetForm("ks_value") != nil {
		data["ks_value"] = r.GetFormFloat64("ks_value")
	}

	if r.GetForm("cz") != nil {
		data["cz"] = r.GetFormUint("cz")
	}

	if r.GetForm("cz_value") != nil {
		data["cz_value"] = r.GetFormFloat64("cz_value")
	}

	if r.GetForm("qx") != nil {
		data["qx"] = r.GetFormUint("qx")
	}

	result := service.SubjectService().UpdateSubject(subject_id, data)

	response.JsonExit(r, 0, "ok", result)
}

func CreateSubject(r *ghttp.Request) {

	school_id := r.GetQueryUint("school_id")
	campus_id := r.GetQueryUint("campus_id")

	var (
		data         *define.SubjectRequest
		subjectParam *model.Subject
	)

	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	if err := gconv.Struct(data, &subjectParam); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result := service.SubjectService().CreateSubject(school_id, campus_id, subjectParam)

	response.JsonExit(r, 0, "ok", result)
}
